<template>
	<view>
		<view class="goods-list">
			<view v-for="(item, idx) in goodsList" :key="idx" @click="gotoDetail(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: "",
					cid: "",
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				//默认图片
				defaultPic:"http://image2.suning.cn/uimg/b2c/newcatentries/0070137264-000000000170759033_2_800x800.jpg",
				//是否正在请求数据
				isLoading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ""
			this.queryObj.cid = options.cid || ""
			this.getGoodsList()
		},
		methods: {
			gotoDetail(item) {
				uni.navigateTo({
					url: "/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id
				})
			},
			//获取商品列表数据
			async getGoodsList(cb) {
				this.isLoading = true
				let {data: res} = await uni.$http.get("/api/public/v1/goods/search", this.queryObj)
				this.isLoading = false
				cb && cb()
				if(res.meta.status != 200) return uni.$showMsg()
				this.total = res.message.total
				this.goodsList = [...this.goodsList, ...res.message.goods]
			}
		},
		//页面滑动触底事件
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
				uni.$showMsg("数据加载完毕")
				return
			} 
			if(this.isLoading) return
			//让页码值自增加1
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		//下拉刷新事件
		onPullDownRefresh() {
			//重置数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			//重新发起数据请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
.goods-item {
	display: flex;
	padding: 10px 5px;
	border-bottom: 1px solid #CCC;
	.goods-item-left {
		margin-right: 5px;
		.goods-pic {
			display: block;
			width: 100px;
			height: 100px;
		}
	}
	.goods-item-right {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.goods-name {
			font-size: 13px;
		}
		.goods-info-box {
			.goods-price {
				color: #C00000;
				font-size: 16px;
			}
		}
	}
}
</style>
